package com.jrx.anytxn.reporting.job.xib.config;

import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.param.utils.BizTypeProducts;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.entity.AmRepaymentPlanInfo;
import com.jrx.anytxn.reporting.entity.CmInteCoLonRepayPlan;
import com.jrx.anytxn.reporting.job.xib.step.RepayPlanReportingListener;
import com.jrx.anytxn.reporting.job.xib.step.RepayPlanReportingProcessor;
import com.jrx.anytxn.reporting.job.xib.step.RepayPlanReportingWriter;
import com.jrx.anytxn.reporting.job.xib.step.RepayPlanXajReportingProcessor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @comment: 贷款还款计划报表任务定义
 * @author: hsoong
 * @date: 2021/7/1 21:26
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class RepayPlanReportingConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;

    @Autowired
    RepayPlanReportingListener repayPlanReportingListener;

    @Autowired
    RepayPlanReportingProcessor repayPlanReportingProcessor;

    @Autowired
    RepayPlanXajReportingProcessor repayPlanXajReportingProcessor;

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    RepayPlanReportingWriter repayPlanReportingWriter;

    @Bean
    public Job xibRepayPlanReportingJob() {
        return jobBuilderFactory.get("xibRepayPlanReportingJob")
                .incrementer(new RunIdIncrementer())
                .listener(repayPlanReportingListener)
                .start(xibRepayPlanReportingJobStep())
                .next(xibRepayPlanXajReportingJobStep())
                .build();
    }

    @Bean
    public Step xibRepayPlanReportingJobStep() {
        return stepBuilderFactory.get("xibRepayPlanReportingJobStep")
                .<CmCustomerFirstLevel, CmInteCoLonRepayPlan>chunk(anyTxnBatchProperties.getReportingCustomerConfig().getChunkLimit())
                .reader(repayPlanReportingReader())
                .processor(repayPlanReportingProcessor)
                .writer(repayPlanReportingWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingCustomerConfig().getThrottleLimit())
                .build();
    }

    /**
     * @description: 贷款还款计划报表任务定义  (兴安借)
     * @author: shigongtai
     * @date: 2023/4/13 14:29
     */
    @Bean
    public Step xibRepayPlanXajReportingJobStep() {
        return stepBuilderFactory.get("xibRepayPlanXajReportingJobStep")
                .<AmRepaymentPlanInfo, CmInteCoLonRepayPlan>chunk(anyTxnBatchProperties.getReportingCustomerConfig().getChunkLimit())
                .reader(repayPlanReaderXaj())
                .processor(repayPlanXajReportingProcessor)
                .writer(repayPlanReportingWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingCustomerConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<CmCustomerFirstLevel> repayPlanReportingReader() {
        MyBatisCursorItemReader<CmCustomerFirstLevel> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        cursorItemReader.setQueryId("com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerFirstLevelMapper.selectAllCust");
        SynchronizedItemStreamReader<CmCustomerFirstLevel> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<AmRepaymentPlanInfo> repayPlanReaderXaj() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
        MyBatisCursorItemReader<AmRepaymentPlanInfo> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        //兼容信用就医 用getAllRepayPlanCiWithProducts替换getAllRepayPlanCi
        cursorItemReader.setQueryId("com.jrx.anytxn.reporting.mapper.ext.ExtFtpInteCoLonMapper.getAllRepayPlanCiWithProducts");
        Map<String, Object> map = new HashMap<>();
        map.put("accountStatus", Arrays.asList(ReportingConstant.ACCOUNT_STATUS_1, ReportingConstant.ACCOUNT_STATUS_2));
        map.put("payOffStatus", Arrays.asList(ReportingConstant.ACCOUNT_STATUS_6, ReportingConstant.ACCOUNT_STATUS_7, ReportingConstant.ACCOUNT_STATUS_8));
        //map.put("loanType", ReportingConstant.LOAN_TYPE_4);
        map.put("today", org.getToday());
        map.put("productIds", BizTypeProducts.getXAJSeriesProducts());
        cursorItemReader.setParameterValues(map);
        SynchronizedItemStreamReader<AmRepaymentPlanInfo> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }
}
